package com.shigu.admin.diy.actions;

import com.shigu.admin.diy.bo.DiyPageBO;
import com.shigu.admin.diy.bo.FuncAddBo;
import com.shigu.admin.diy.bo.FuncQueryBO;
import com.shigu.admin.diy.bo.PageQueryBo;
import com.shigu.admin.diy.services.PageService;
import com.shigu.admin.diy.services.FunctionService;
import com.shigu.admin.manager.common.JSONResult;
import com.shigu.main4.common.exceptions.JsonErrException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 自定义页面管理
 * Created by bugzy on 2017/7/10 0010.
 */
@Controller
@RequestMapping("diy/")
public class PageListAction {

    @Autowired
    private PageService pageService;

    @Autowired
    private FunctionService functionService;

    /**
     * 页面管理
     */
    @RequestMapping("pages")
    @RequiresPermissions("diy:pages")
    public String pages(PageQueryBo bo, Model model) {
        model.addAttribute("pager",pageService.queryPages(bo));
        model.addAttribute("bo", bo);
        return "diy/pages";
    }

    @RequestMapping(value = "newpage", method = RequestMethod.GET)
    @RequiresPermissions("diy:newpage")
    public String newPage(Integer type, Long id, Model model) {
        model.addAttribute("type", type);
        if (id != null) {
            model.addAttribute("page", pageService.selDiyPageByIdAndType(id, type));
        }
        return "diy/newpage";
    }

    @RequestMapping(value = "newpage", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("diy:newpage")
    public JSONResult doNewPage(DiyPageBO bo) throws JsonErrException {
        pageService.addPage(bo);
        return JSONResult.success();
    }

    @RequestMapping("delPages")
    @RequiresPermissions("diy:delpage")
    @ResponseBody
    public JSONResult delPages(Long pageId){
        pageService.delPage(pageId);
        return JSONResult.success();
    }

    //******************************************************************************************************************

    /**
     * 功能管理
     */
    @RequestMapping("funcs")
    @RequiresPermissions("diy:funcs")
    public String functions(FuncQueryBO bo, Model model) {
        model.addAttribute("bo", bo);
        model.addAttribute("pager", functionService.queryPages(bo));
        return "diy/funcs";
    }

    @RequestMapping(value = "addfunc", method = RequestMethod.GET)
    @RequiresPermissions("diy:addfunc")
    public String addFunc(Long id, Model model) {
        if (id != null) {
            model.addAttribute("func", functionService.queryById(id));
        }
        return "diy/addfunc";
    }

    @RequestMapping(value = "addfunc", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("diy:addfunc")
    public JSONResult addFunc(FuncAddBo bo) throws JsonErrException {
        functionService.addFunc(bo);
        return JSONResult.success();
    }

    @RequestMapping("delfunc")
    @RequiresPermissions("diy:delfunc")
    @ResponseBody
    public JSONResult delfunc(Long funcId) {
        functionService.delById(funcId);
        return JSONResult.success();
    }

}
